﻿layui.use(function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;

    // 事件
    util.on('lay-on', {
        'test-page-new': function () {
            let selectedRows = [];
            // 创建弹出层
            var index = layer.open({
                type: 1,
                title: '产品信息',
                area: ['1100px', '500px'], // 宽高
                content: '<form class="layui-form layui-row layui-col-space16" id="IDProdurement">' +
                    '<div class="layui-col-md4">' +
                    '<label class="layui-form-label" style="width: 120px">产品编码:</label>' +
                    '<div class="layui-input-block">' +
                    '<input type="text" id="productsCode" placeholder="产品编码" style="width:200px;" class="layui-input" lay-affix="clear">' +
                    '</div>' +
                    '</div>' +
                    '<div class="layui-col-md4">' +
                    '<label class="layui-form-label" style="width: 120px">产品名称:</label>' +
                    '<div class="layui-input-block">' +
                    '<input type="text" name="B" placeholder="产品名称" id="productsName" style="width:200px;" lay-affix="clear" class="layui-input">' +
                    '</div>' +
                    '</div>' +
                    '<div class="layui-btn-container layui-col-md4">' +
                    '<button type="button" class="layui-btn" id="searchBtn">查询</button>' +
                    '</div> ' +
                    '</form>' +
                    '<table class="layui-table" lay-filter="Produrement-Id" id="Produrement-Id">' +
                    '<thead>' +
                    '<tr>' +
                    '<th lay-data="{checkbox:true}" rowspan="2"></th>' +
                    '<col width="150">' +
                    '<col>' +
                    '</tr>' +
                    '</thead>' +
                    '</table>' +
                    '<button type="button" style="margin-top: 20px;margin-left: 500px;" id="demo-login"  class="layui-btn layui-bg-blue">提交</button>'
            });
            form.render();

            // 弹出层弹出时显示数据
            getProcurementList({
                productsCode: null,
                productsName: null,
                pageIndex: 1,
                pageSize: 4
            });

            // 查询
            $("#searchBtn").on('click', function () {
                var productsCode = $("#productsCode").val();
                var productsName = $("#productsName").val();

                getProcurementList({
                    productsCode: productsCode,
                    productsName: productsName,
                    pageIndex: 1,
                    pageSize: 4
                });
            });

            // 调用入库产品显示方法
            function getProcurementList({ productsCode, productsName, pageIndex, pageSize }) {
                $.ajax({
                    url: '/EnterStorage/EnterStorageSelect',
                    type: 'get',
                    toolbar: '#toolbarDemo',
                    data: {
                        StockCode: null,
                        StockOrderType: null,
                        StateSP: null,
                        productsCode: productsCode,
                        productsName: productsName,
                        pageIndex: pageIndex,
                        pageSize: pageSize,
                        Token: localStorage.getItem("token")
                    },
                    success: function (res) {
                        var data = JSON.parse(res);
                        table.render({
                            elem: '#Produrement-Id',
                            totalRow: true, // 开启合计行
                            cols: [[
                                { type: 'checkbox', fixed: 'left' },
                                { field: 'productsCode', width: 120, title: '产品编码' },
                                { field: 'productsName', width: 120, title: '产品名称' },
                                { field: 'specs', width: 120, title: '规格' },
                                { field: 'productTypeName', width: 120, title: '类别' },
                                { field: 'specs', width: 120, title: '存储单位' },
                                {
                                    width: 120, title: '包装类型', templet: function (d) {
                                        return '1'
                                    }
                                },
                                { field: 'sprice', width: 120, title: '单价' },
                                {
                                    width: 120, title: '重量', templet: function (d) {
                                        return '0'
                                    }
                                },
                                { width: 120, title: '显示名' },
                            ]],
                            data: data.data.list,//数据源
                            page: true,   // 启用分
                            limit: 5,    // 每页显示多少条数据
                            limits: [10, 20, 30],  // 分页选择项
                            curr: 1,  // 当前页
                            total: data.data.sumcount,  // 总记录数
                            loading: true, // 启用加载状态
                            toolbar: '#toolbarDemo',
                            defaultToolbar: ['exports'],
                        })
                    }

                })
            }
            // 工具栏事件
            var data;
            table.on('checkbox', function (obj) {
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                data = checkStatus.data; // 获取选中的数据
                //console.log(data);
            });
            //提交关闭弹出层
            $("#demo-login").on('click', function () {
                layer.close(index);//关闭弹窗
                var selectdids = data.map(function (item) {
                    return item.stockProductId;
                })
                //console.log(selectdids);
                var dto = {
                    ids: selectdids,
                    Token: localStorage.getItem("token")
                }
                $.ajax({
                    url: "/Stocktaking/SelectInventoryProduce",
                    type: 'post',
                    data: dto,
                    success: function (res) {
                        var s = JSON.parse(res);
                        //console.log(s.data);
                        //渲染表格
                        table.render({
                            elem: '#ID-demo',
                            toolbar: '#toolbarDemo',
                            cols: [[
                                { type: 'checkbox', fixed: 'left' },
                                { field: 'productName', width: 180, title: '产品名称' },
                                { field: 'productCode', width: 180, title: '产品编码' },
                                { field: 'specifications', width: 180, title: '规格' },
                                { field: 'batch', width: 180, title: '批次' },
                                { field: 'unit', width: 180, title: '单位' },
                                { field: 'warehouseLocation', width: 180, title: '库位' },
                                //{
                                //    field: 'stockProductId', width: 195, title: '操作', templet: function (d) {
                                //        return '<a class="layui-icon layui-icon-delete" lay-on="test-page-custom" style="color:blue" id="DelProduct" data-pandian="' + d.stockProductId + '">删除</a>'
                                //    }
                                //},
                            ]],
                            data: s.data
                        });

                    }
                })
            })
            $("#addpan").on('click', function () {
                var currentDate = new Date();

                // 生成6位数
                var randomNum = Math.floor(100000 + Math.random() * 900000);  // 生成100000到999999之间的随机整数
                var selectdids = data.map(function (item) {
                    //console.log(item);
                    var dto1 = {
                        Number: item.quantity,
                        InventProductIdId: item.inventProductIdId,
                        InventoryId: item.inventoryId,
                        ProductId: item.productId,
                        ProductName: item.productName,
                        ProductCode: item.productCode,
                        Specifications: item.specifications,
                        Batch: item.batch,
                        Unit: item.specifications,
                        WarehouseLocation: item.warehouseLocation,
                        office_id: "1",
                        user_id: 1,
                        addName: "卢芳",
                        addTime: currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate() + ' ' + currentDate.getHours() + ':' + currentDate.getMinutes(),
                    }
                    return dto1;
                })


                var dto = {
                    inventoryCode: randomNum,
                    morderNumber: $("#MorderNumber").val(),
                    makePerson: $("#MakePerson").val(),
                    markTime: $("#MarkTime").val(),
                    iRremark: $("#IRremark").val(),
                    inventoryState: 1,
                    inventoryType: "产品盘点",
                    office_id: "1",
                    user_id: 1,
                    addName: "卢芳",
                    addTime: currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate() + ' ' + currentDate.getHours() + ':' + currentDate.getMinutes(),
                    addInventGuan: selectdids,
                    Token: localStorage.getItem("token")
                };

                console.log(dto);
                $.ajax({
                    url: "/Stocktaking/ADDStockings",
                    type: 'post',
                    data: dto,
                    success: function (res) {
                        var s = JSON.parse(res);
                        if (s.data == 1) {
                            layer.msg('添加成功', { icon: 0 }, function () {
                                //location.reload()
                                location.href = "/Stocktaking/Stocking";
                            });
                        }
                        else if (s.data == -1) {
                            layer.msg('类别名称已存在，不能添加');
                            return
                        }
                        else {
                            layer.msg('添加失败');
                            return
                        }
                    }
                })



            })



















        }
    });
});